<?php

namespace frontend\modules\orders\models;

use backend\modules\shipping\models\EcsRegion;
use backend\modules\shipping\models\Shipping;
use common\models\Wxuser;
use frontend\modules\address\models\UserAddress;
use frontend\modules\marketing\models\Coupon;
use Yii;
use yii\base\Exception;

/**
 * This is the model class for table "order_info".
 *
 * @property string $order_id
 * @property string $order_sn
 * @property string $user_id
 * @property integer $order_status
 * @property integer $shipping_status
 * @property integer $pay_status
 * @property string $consignee
 * @property integer $country
 * @property integer $province
 * @property integer $city
 * @property integer $district
 * @property string $address
 * @property string $tel
 * @property string $mobile
 * @property string $email
 * @property string $best_time
 * @property integer $shipping_id
 * @property string $shipping_name
 * @property integer $pay_id
 * @property string $pay_name
 * @property string $goods_amount
 * @property string $shipping_fee
 * @property string $pay_fee
 * @property string $money_paid
 * @property string $surplus
 * @property string $integral
 * @property string $integral_money
 * @property string $bonus
 * @property string $order_amount
 * @property integer $from_ad
 * @property string $referer
 * @property string $add_time
 * @property string $confirm_time
 * @property string $pay_time
 * @property string $shipping_time
 * @property integer $pack_id
 * @property integer $card_id
 * @property string $bonus_id
 * @property string $invoice_no
 * @property string $to_buyer
 * @property string $parent_id
 * @property string $discount
 * @property string $transaction_id
 * @property string $shopping_temp
 * @property integer $updated_at
 * @property integer $comment_status
 * @property integer $is_delete
 * @property string $order_check_money
 * @property string $order_user_money
 * @property string $order_should_check_money
 * @property integer $is_without_pay
 * @property integer $cancel_status
 * @property string $user_name
 * @property integer $created_at
 * @property integer $is_share
 * @property integer $is_warm
 * @property integer $coupon_money
 */
class OrderInfo extends \yii\db\ActiveRecord
{
    public $country_name;
    public $province_name;
    public $city_name;
    public $district_name;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order_info';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
//            TimestampBehavior::className(),
            // 名为myBehavior4的行为，也是给出了MyBehavior类的配置数组
            'uuid' => [
                'class' => \common\behavior\uuidBehavior::className(),
                'owner' => $this
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['order_id','default','value'=>$this->getUUID()],
            ['order_sn','default','value'=>$this->createSn()],
            [['order_id'], 'required'],
            [['order_status', 'is_warm','is_share','shipping_status', 'pay_status', 'country', 'province', 'city', 'district', 'shipping_id', 'pay_id', 'add_time', 'confirm_time', 'shipping_time', 'parent_id', 'updated_at', 'comment_status', 'is_delete', 'is_without_pay', 'cancel_status', 'created_at'], 'integer'],
            [['goods_amount', 'shipping_fee', 'pay_fee', 'money_paid', 'order_amount', 'discount', 'order_check_money', 'order_user_money', 'order_should_check_money','coupon_money'], 'number'],
            [['shopping_temp'], 'string'],
            [['order_id', 'user_id'], 'string', 'max' => 40],
            [['order_sn'], 'string', 'max' => 20],
            [['consignee', 'tel', 'mobile', 'email', 'user_name'], 'string', 'max' => 60],
            [['address', 'referer', 'invoice_no', 'to_buyer'], 'string', 'max' => 255],
            [['best_time', 'shipping_name', 'pay_name'], 'string', 'max' => 120],
            [['pay_time'], 'string', 'max' => 50],
            [['transaction_id'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'order_id' => Yii::t('app', 'Order ID'),
            'order_sn' => Yii::t('app', '订单号'),
            'user_id' => Yii::t('app', 'User ID'),
            'user_name'=>Yii::t('app', '用户'),
            'order_status' => Yii::t('app', '订单状态(0未支付、 1已支付待发货、2已支付已发货待收货、3已支付已发货已收货待评价、4支付已发货已收货已评价、5未支付已取消、6已删除)'),
            'shipping_status' => Yii::t('app', '发货状态'),
            'pay_status' => Yii::t('app', '是否支付'),
            'comment_status' => Yii::t('app', '是否评论'),
            'cancel_status' => Yii::t('app', '是否已取消'),
            'is_delete' => Yii::t('app', '是否删除'),
            'consignee' => Yii::t('app', '收货人'),
            'country' => Yii::t('app', '国家'),
            'province' => Yii::t('app', '省份'),
            'city' => Yii::t('app', '城市'),
            'district' => Yii::t('app', '区域'),
            'address' => Yii::t('app', '详细地址'),
            'tel' => Yii::t('app', '电话号码'),
            'mobile' => Yii::t('app', '手机号码'),
            'email' => Yii::t('app', 'Email'),
            'best_time' => Yii::t('app', '送货最佳时间'),
            'shipping_id' => Yii::t('app', 'Shipping ID'),
            'shipping_name' => Yii::t('app', '物流方式'),
            'pay_id' => Yii::t('app', 'Pay ID'),
            'pay_name' => Yii::t('app', '支付方式'),
            'goods_amount' => Yii::t('app', '订单实际需支付总价,满减后等等'),
            'shipping_fee' => Yii::t('app', '邮费'),
            'pay_fee' => Yii::t('app', 'Pay Fee'),
            'money_paid' => Yii::t('app', '实际已支付总价'),
            'surplus' => Yii::t('app', 'Surplus'),
            'order_amount' => Yii::t('app', '订单正常总价'),
            'referer' => Yii::t('app', 'Referer'),
            'add_time' => Yii::t('app', '下单时间'),
            'confirm_time' => Yii::t('app', 'Confirm Time'),
            'pay_time' => Yii::t('app', 'Pay Time'),
            'shipping_time' => Yii::t('app', 'Shipping Time'),
            'invoice_no' => Yii::t('app', '物流号'),
            'to_buyer' => Yii::t('app', 'To Buyer'),
            'parent_id' => Yii::t('app', 'Parent ID'),
            'discount' => Yii::t('app', '折扣'),
            'transaction_id' => Yii::t('app', '支付编号'),
            'shopping_temp' => Yii::t('app', '电子面单'),
            'updated_at' => Yii::t('app', '更新时间'),
            'created_at' => Yii::t('app', '下单时间'),
            'order_check_money' => Yii::t('app', 'Order Check Money'),
            'order_user_money' => Yii::t('app', 'Order User Money'),
            'order_should_check_money' => Yii::t('app', 'Order Should Check Money'),
            'is_without_pay' => Yii::t('app', '是否不用支付'),
            'is_share'=>Yii::t('app', '是否已分享'),
        ];
    }

    /**
     * @inheritdoc
     * @return \backend\modules\orders\models\query\OrderInfoQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \backend\modules\orders\models\query\OrderInfoQuery(get_called_class());
    }

    /**
     * (0未支付、 1已支付待发货、2已支付已发货待收货、3已支付已发货已收货待评价、4支付已发货已收货已评价、5未支付已取消、6已删除)
     * @param $type
     * @return array|string
     */
    public static function orderStatusAlias($type)
    {
        if(empty($type)){
            return "未支付";
        } else if($type==1){
            return "已支付、待发货";
        }else if($type==2){
            return "已支付、已发货、待收货";
        }else if($type==3){
            return "待评价";
        }else if($type==4){
            return "已评价";
        }else if($type==5){
            return "已取消";
        }else if($type==6){
            return "已删除";
        }else if($type=='all'){
            return ["未支付","已支付、待发货","已支付、已发货、待收货","待评价","已评价","已取消","已删除",];
        }
    }

    /**
     * 获取物流列表，订单物流选中
     *
     * @param $id
     * @return array|\backend\modules\shipping\models\Shipping[]
     */
    public function getShippingByOrderId($id)
    {
        $order = OrderInfo::findOne($id);
        $shipping = Shipping::find()->asArray()->all();
        $shippingARR = array();
        if($order->shipping_id){
            foreach($shipping as $k=>$v) {
                if($v['shipping_id']==$order->shipping_id){
                    $v['check'] = 1;
                    $v['invoice_no'] = $order->invoice_no;
                    $v['shipping_temp'] = $order->shopping_temp;
                } else {
                    $v['check'] = 0;
                    $v['invoice_no'] = '';
                    $v['shipping_temp'] = '';
                }
                $shippingARR[$k] = $v;
            }
        } else {
            return $shipping;
        }
        return $shippingARR;
    }

    public function afterFind()
    {
        $addec = new \backend\modules\orders\lib\AddExchange();
        $addec->exchangeObj($this);
    }

    /**
     * 生成订单头
     *
     * @param $postData
     * @return string
     * @throws Exception
     */
    public static function createOrder($postData)
    {
        $orderInfo =  new OrderInfo();
        $userid = Yii::$app->UserInfo->userid;
        $orderInfo->user_id = $userid;
        $orderInfo->country = $postData['country'];
        $orderInfo->province = $postData['province'];
        $orderInfo->city =  $postData['city'];
        $orderInfo->district = $postData['district'];
        $orderInfo->address = $postData['address'];
        $orderInfo->pay_id = $postData['pay_id'];
        $orderInfo->pay_name = $postData['pay_name'];
        $orderInfo->coupon_money = $postData['coupon_money'];
        $orderInfo->order_amount = $postData['order_amount'];
        $orderInfo->goods_amount = $postData['order_amount']-$postData['coupon_money'];
        $orderInfo->consignee = $postData['consignee'];
        $orderInfo->tel = $postData['tel'];
        $orderInfo->email = $postData['email'];
        $orderInfo->add_time = time();

        if($orderInfo->save()) {
            return $orderInfo->order_id;
        }
        throw new Exception(json_encode($orderInfo->errors));
    }

    /**
     * 创建订单号
     *
     * @return string
     */
    public function createSn()
    {
        $time = time();
        $sn = date("Ymd",$time).rand(99,10000);
        if(static::findOne(['order_sn'=>$sn])){
            $this->createSn();
        }
        return $sn;
    }

    /**
     * 获取当前用户默认地址
     *
     * @param $userid
     * @return static
     */
    public function getUserAddress($userid)
    {
        $address_id = Wxuser::findOne($userid)->address_id;
        $address = UserAddress::findOne($address_id);
        return $address;
    }

    /**
     * 获取所有参与活动的商品信息
     *
     * @return array
     */
    public function MyActive()
    {
        $userid = Yii::$app->UserInfo->userid;
        $ordergoods = OrderGoods::find()->where(['user_id'=>$userid])->asArray()->all();
        $allActive = array();
        foreach($ordergoods as $k=>$v) {
            if($v['refere_id']){
                $allActive[] = $v;
            }
        }
        return $allActive;
    }
}
